package com.wswy.wzcx.push;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.baidu.mobstat.MtjConfig;
import com.blankj.utilcode.util.ActivityUtils;
import com.umeng.message.UmengNotifyClickActivity;
import com.umeng.message.entity.UMessage;
import com.wswy.wzcx.BuildConfig;
import com.wswy.wzcx.ui.activity.SplashActivity;
import com.wswy.wzcx.ui.main.MainActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.android.agoo.common.AgooConstants;
import org.json.JSONObject;

public class TDPushActivity extends UmengNotifyClickActivity {

    private static final String EXTRA_TARGET = "target";
    private static final String EXTRA_URL = "url";
    private static final String EXTRA_TITLE = "title";
    private static final String EXTRA_USER_CAR_ID = "userCarId";
    private static final String EXTRA_BBS_ID = "bbsId";
    private static final String EXTRA_FINE_NO = "violation";

    private static final String TAG = "TDPushActivity";

    private boolean handled = false;
    private Handler handler = null;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        handler = new Handler();
        handleIntent(getIntent());

    }

    private Runnable waitFinish = new Runnable() {
        @Override
        public void run() {
            finish();
        }
    };

    @Override
    public void onMessage(Intent intent) {
        if (intent != null) {
            super.onMessage(intent);
            handleIntent(intent);
        } else {
            //Log.e(TAG, "onMessage --> intent is null");
        }

    }


    private void handleIntent(Intent intent) {
        if (intent != null) {
            if (BuildConfig.DEBUG) {
                Bundle extras = intent.getExtras();
                if (extras != null) {
                    for (String s : extras.keySet()) {
                        Log.e(TAG, "handleIntent --> " + s + "  " + extras.get(s));
                    }
                } else {
                    Log.e(TAG, "handleIntent --> extras is null!");
                }
            }

            try {
                String body = intent.getStringExtra(AgooConstants.MESSAGE_BODY);
                //Log.e(TAG, "handleIntent --> body:"+body);
                if (!TextUtils.isEmpty(body)) {
                    UMessage uMessage = new UMessage(new JSONObject(body));
                    handleMessage(uMessage.extra);
                } else if (intent.getExtras().containsKey(EXTRA_TARGET)) {
                    Map<String, String> map = new HashMap<>();
                    map.put(EXTRA_TARGET, intent.getStringExtra(EXTRA_TARGET));
                    map.put(EXTRA_URL, intent.getStringExtra(EXTRA_URL));
                    map.put(EXTRA_TITLE, intent.getStringExtra(EXTRA_TITLE));
                    map.put(EXTRA_USER_CAR_ID, intent.getStringExtra(EXTRA_USER_CAR_ID));
                    map.put(EXTRA_FINE_NO, intent.getStringExtra(EXTRA_FINE_NO));
                    handleMessage(map);
                } else {
                    handler.removeCallbacks(waitFinish);

                    handler.postDelayed(waitFinish, 3000);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Log.e(TAG, "handleIntent --> intent is null");
        }
    }

    private void handleMessage(Map<String, String> message) {
        if (handled) {
            finish();
            return;
        }

        List<Activity> activityList = ActivityUtils.getActivityList();
        //判断是不是冷启动页，只需要判断第一个页面是不是自己
        boolean isAlive = activityList != null && !activityList.isEmpty() && activityList.get(0) != this;

        if (BuildConfig.DEBUG) {
            Log.e(TAG, "handleMessage -->activityList " + activityList);
        }

        Intent intent = null;

        if (isAlive) {
            intent = new Intent(this, MainActivity.class);
        } else {
            intent = new Intent(this, SplashActivity.class);
        }
        if (BuildConfig.DEBUG) {
            Log.e(TAG, "handleMessage --> " + message);
        }

        if (message != null && !message.isEmpty()) {
            intent.putExtra(PushExtras.TARGET, message.get(EXTRA_TARGET));
            intent.putExtra(PushExtras.TITLE, message.get(EXTRA_TITLE));
            intent.putExtra(PushExtras.URL, message.get(EXTRA_URL));
            intent.putExtra(PushExtras.USER_CAR_ID, message.get(EXTRA_USER_CAR_ID));
            intent.putExtra(PushExtras.TOPIC_ID, message.get(EXTRA_BBS_ID));
            intent.putExtra(PushExtras.FINE_NO, message.get(EXTRA_FINE_NO));
        }
        //启动来源分析
        intent.putExtra(MtjConfig.BAIDU_MTJ_PUSH_CALL, true);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        handled = true;
        overridePendingTransition(0, 0);
        finish();
    }

}
